package com.qen.interceptor;

import cn.hutool.core.date.DateUtil;
import com.qen.service.IService;
import com.qen.service.ServiceImpl;
import org.beetl.sql.core.InterceptorContext;
import org.beetl.sql.ext.DebugInterceptor;

/**
 * 控制台输出SQL执行时间和SQL执行调用类
 * @author licz
 * @title DataSourceConfig
 * @date 2024/8/8 10:43
 * @description beelsql 日志配置
 */
public class CustomLogInterceptor extends DebugInterceptor {

    protected static String serviceImpl = ServiceImpl.class.getName();
    protected static String iService = IService.class.getName();

    @Override
    public void before(InterceptorContext ctx) {
        String now = DateUtil.now();
        this.println("✰SQL执行时间：" + now + " ✰");
        super.before(ctx);
    }

    @Override
    protected int lookBusinessCodeInTrace(StackTraceElement[] traces) {
        String className = getTraceClassName();
        int sqlMangerIndex = -1;
        for (int i = traces.length - 1; i >= 0; i--) {
            String name = traces[i].getClassName();
            if (className != null && className.equals(name)) {
                return i;
            } else if (name.equals(sqlManager)) {
                sqlMangerIndex = i + 1;
            }
        }
        int i = sqlMangerIndex;
        try {
            for (; i < traces.length - 1; i++) {
                String name = traces[i].getClassName();
                if (name.equals(serviceImpl) || name.equals(iService)) {
                    for (i = i + 1; i < traces.length; i++) {
                        name = traces[i].getClassName();
                        if (!name.startsWith("com.qen")
                                && !name.startsWith("org.springframework")
                                && !name.startsWith("org.beetl.sql")
                                && !name.startsWith("jdk")
                                && !name.startsWith("sun")
                        ) {
//                            if (traces[i].getClassName().indexOf("$") > -1) {
//                                return i + 1;
//                            }
                            return i;
                        }
                    }
                    return i + 1;
                }
//                else if (name.equals(mapperName)) {
//                    for (i = i + 1; i < traces.length; i++) {
//                        name = traces[i].getClassName();
//                        if (name.indexOf("$Proxy") != -1 && traces[i].getLineNumber() == -1) {
//                            //mapper代理类
//                            return i + 1;
//                        }
//                    }
//                    return i + 1;
//                } else if (name.startsWith("org.beetl.sql.core.query")) {
//                    for (i = i + 1; i < traces.length; i++) {
//                        name = traces[i].getClassName();
//                        if (!name.startsWith("org.beetl.sql.core.query")) {
//                            return i;
//                        }
//                    }
//                }
            }
            return sqlMangerIndex;
        } finally {
            if (i == traces.length) {
                //遍历完毕还未找到，可能是bug，返回一个保守位置。这可能是未来包名变化导致
                return sqlMangerIndex;
            }
        }
    }
}
